home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / ghostscript / src / zht.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  4KB  |  153 lines

  1. /* Copyright (C) 1989, 1991 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* zht.c */
  20. /* Halftone operators and rendering for Ghostscript */
  21. #include "ghost.h"
  22. #include "memory_.h"
  23. #include "errors.h"
  24. #include "oper.h"
  25. #include "alloc.h"
  26. #include "estack.h"
  27. #include "gsmatrix.h"
  28. #include "gsstate.h"
  29. #include "state.h"
  30. #include "store.h"
  31.  
  32. /* Forward references */
  33. private int screen_sample(P1(os_ptr));
  34. private int set_screen_continue(P1(os_ptr));
  35. private int screen_cleanup(P1(os_ptr));
  36.  
  37. /* - currentscreen <frequency> <angle> <proc> */
  38. /* - currentscreen 60 0 <dict> */
  39. int
  40. zcurrentscreen(register os_ptr op)
  41. {    if ( r_has_type(&istate.halftone, t_null) )
  42.     {    /* Screen was set by setscreen. */
  43.         float freq, angle;
  44.         float (*proc)(P2(floatp, floatp));
  45.         gs_currentscreen(igs, &freq, &angle, &proc);
  46.         push(3);
  47.         make_real(op - 2, freq);
  48.         make_real(op - 1, angle);
  49.         *op = istate.screen_procs.gray;
  50.     }
  51.     else
  52.     {    /* Screen was set by sethalftone. */
  53.         push(3);
  54.         make_real(op - 2, 60);
  55.         make_real(op - 1, 0);
  56.         *op = istate.halftone;
  57.     }
  58.     return 0;
  59. }
  60.  
  61. /* The setscreen operator is complex because it has to sample */
  62. /* each pixel in the pattern cell, calling a procedure, and then */
  63. /* sort the result into a whitening order. */
  64.  
  65. /* Layout of stuff pushed on estack: */
  66. /*    Control mark, */
  67. /*    spot procedure, */
  68. /*    enumeration structure (as bytes). */
  69. #define snumpush 3
  70. #define sproc esp[-1]
  71. #define senum (gs_screen_enum *)esp->value.bytes
  72.  
  73. /* <frequency> <angle> <proc> setscreen - */
  74. int
  75. zsetscreen(register os_ptr op)
  76. {    float fa[2];
  77.     int code = num_params(op - 1, 2, fa);
  78.     gs_screen_enum *penum;
  79.     if ( code < 0 ) return code;
  80.     check_proc(*op);
  81.     check_estack(snumpush);
  82.     penum = (gs_screen_enum *)alloc(1, gs_screen_enum_sizeof, "setscreen");
  83.     if ( penum == 0 )
  84.         return_error(e_VMerror);
  85.     /* Push everything on the estack */
  86.     make_mark_estack(esp + 1, es_other, screen_cleanup);
  87.     esp[2] = *op;            /* sproc = proc */
  88.     make_tasv(esp + 3, t_string, 0, gs_screen_enum_sizeof, bytes, (byte *)penum);
  89.     code = gs_screen_init(penum, igs, fa[0], fa[1]);
  90.     if ( code < 0 )
  91.        {    screen_cleanup(op);
  92.         return code;
  93.        }
  94.     esp += snumpush;
  95.     pop(3);  op -= 3;
  96.     return screen_sample(op);
  97. }
  98. /* Set up the next sample */
  99. private int
  100. screen_sample(register os_ptr op)
  101. {    gs_screen_enum *penum = senum;
  102.     gs_point pt;
  103.     int code = gs_screen_currentpoint(penum, &pt);
  104.     ref proc;
  105.     if ( code < 0 ) return code;
  106.     if ( code != 0 )
  107.        {    /* All done */
  108.         istate.screen_procs.red = sproc;
  109.         istate.screen_procs.green = sproc;
  110.         istate.screen_procs.blue = sproc;
  111.         istate.screen_procs.gray = sproc;
  112.         make_null(&istate.halftone);
  113.         esp -= snumpush;
  114.         screen_cleanup(op);
  115.         return o_pop_estack;
  116.        }
  117.     push(2);
  118.     make_real(op - 1, pt.x);
  119.     make_real(op, pt.y);
  120.     proc = sproc;
  121.     push_op_estack(set_screen_continue);
  122.     *++esp = proc;
  123.     return o_push_estack;
  124. }
  125. /* Continuation procedure for processing sampled pixels. */
  126. private int
  127. set_screen_continue(register os_ptr op)
  128. {    float value;
  129.     int code = num_params(op, 1, &value);
  130.     if ( code < 0 ) return code;
  131.     code = gs_screen_next(senum, value);
  132.     if ( code < 0 ) return code;
  133.     pop(1);  op--;
  134.     return screen_sample(op);
  135. }
  136. /* Clean up after screen enumeration */
  137. private int
  138. screen_cleanup(os_ptr op)
  139. {    alloc_free((char *)esp[snumpush].value.bytes,
  140.            1, gs_screen_enum_sizeof, "screen_cleanup");
  141.     return 0;
  142. }
  143.  
  144. /* ------ Initialization procedure ------ */
  145.  
  146. op_def zht_op_defs[] = {
  147.     {"0currentscreen", zcurrentscreen},
  148.     {"3setscreen", zsetscreen},
  149.         /* Internal operators */
  150.     {"1%set_screen_continue", set_screen_continue},
  151.     op_def_end(0)
  152. };
  153.